import axios from 'axios'
// 创建axios实例
const service = axios.create({
  baseURL: 'http://127.0.0.1:8888/api/private/v1/', // api的请求根目录地址
  timeout: 50000 // 请求超时时间
})

// 请求拦截器 （可以在请求拦截器里面设置token）
service.interceptors.request.use(config => {
  //添加token
  config.headers.Authorization = sessionStorage.getItem('token')
  return config
},
  error => {
    console.log(error)
    Promise.reject(error)
  })

// 响应拦截器
service.interceptors.response.use(res => {
  return res.data
}, error => {
  return Promise.reject(error)
}
)

export const getApi = (url, params) => {
  return new Promise((resolve, reject) => {
    service({
      method: 'get',
      url,
      params
    })
      .then(res => resolve(res))
      .catch(err => reject(err))

  })
}


export const postApi = (url, data, config = {}) => {

  return new Promise((resolve, reject) => {
    service({
      method: 'post',
      url,
      data,
      ...config
    })
      .then(res => resolve(res))
      .catch(err => reject(err))

  })
}
